package com.lkd.shoppingcart.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.lkd.shoppingcart.entity.Cart;
import com.lkd.shoppingcart.entity.OrdersDetail;
import com.lkd.shoppingcart.entity.OrdersView;
import com.lkd.shoppingcart.entity.Product;
import com.lkd.shoppingcart.entity.User;
import com.lkd.shoppingcart.service.CartService;
import com.lkd.shoppingcart.service.OrderService;
import com.lkd.shoppingcart.service.ProductService;
import com.lkd.shoppingcart.service.impl.CartServiceImpl;
import com.lkd.shoppingcart.service.impl.OrderServiceImpl;
import com.lkd.shoppingcart.service.impl.ProductServiceImpl;
import com.lkd.shoppingcart.utils.WebUtils;

@WebServlet("/client/orderServlet")
public class ClientOrderServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;
	private OrderService orderService = new OrderServiceImpl();
	private CartService cartService = new CartServiceImpl();
	private ProductService productService = new ProductServiceImpl();
	
	/**
	 * 创建订单
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("user");
		int productId = WebUtils.parseInt(req.getParameter("productId"), 0);        
	    int quantity = WebUtils.parseInt(req.getParameter("quantity"), 1);
		String orderId = orderService.addOrder(user.getId(), productId, quantity);
		session.setAttribute("orderId", orderId);	
		resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");
	}
	
	/**
	 * 根据购物车创建订单
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	protected void createCartOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("user");
		int userId = user.getId();
		Cart cart = cartService.queryByUserId(userId);
		String orderId = orderService.addOrder(cart);
		cart = cartService.queryByUserId(userId);
		List<OrdersView> ordersViewList = orderService.queryByUserId(userId);
		session.setAttribute("cart", cart);
		session.setAttribute("ordersViewList", ordersViewList);
		session.setAttribute("orderId", orderId);			
        resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");
	}
	
	/**
	 * 签收订单
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	protected void receiveOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String orderId = req.getParameter("orderId");
		orderService.updateStatusById(orderId, 2);
		resp.sendRedirect(req.getContextPath()+"/client/orderServlet?action=showOrder");
	}
	
	/**
	 * 签收全部订单
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	protected void receiveAllOrders(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		orderService.updateStatus(1, 2);
		resp.sendRedirect(req.getContextPath()+"/client/orderServlet?action=showOrder");
	}
	
	/**
	 * 获取用户订单信息
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void showOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("user");
		List<OrdersView> ordersViewList = orderService.queryByUserId(user.getId());
		session.setAttribute("ordersViewList", ordersViewList);
		resp.sendRedirect(req.getContextPath()+"/pages/order/order.jsp");
	}
	
	/**
	 * 获取订单详情
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void showOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		String orderId = req.getParameter("orderId");
		List<OrdersDetail> ordersDetailList = orderService.queryForDetail(orderId);
		session.setAttribute("ordersDetailList", ordersDetailList);
		session.setAttribute("orderId", orderId);
		resp.sendRedirect(req.getContextPath()+"/pages/order/orderDetail.jsp");
	}
	
	/**
	 * 检查库存是否足够
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	protected void checkStock(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("user");		
		int userId = user.getId();
		Cart cart = cartService.queryByUserId(userId);
		
		int outStockId = orderService.checkStock(cart);
		
		if (outStockId != 0) {
			// 返回第一个库存不足的商品的名字
			Product product = productService.queryProductById(outStockId);
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("outStockProduct", product.getName());
			
			Gson gson = new Gson();
	        String resultMapJsonString = gson.toJson(resultMap);

	        resp.getWriter().write(resultMapJsonString);
		}
	}
	
}
